package cn.rongdao.jrkabao.ui.login.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxCompoundButton;

import java.util.concurrent.TimeUnit;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.constant.InfoConstant;
import cn.rongdao.jrkabao.ui.login.RegisterActivity;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;

/**
 * Created by jack on 2018/4/20
 */

public class LoginBottomUIComponent extends LinearLayout {

    public LoginBottomUIComponent(Context context) {
        super(context);
    }

    public LoginBottomUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LoginBottomUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private RelativeLayout rlProtocol;
    private CheckBox checkBox;
    private TextView tvLogin;
    private TextView tvRegister;

    private CompositeDisposable compositeDisposable;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        compositeDisposable = new CompositeDisposable();
        TextView tvProtocol = findViewById(R.id.llb_tv_protocol);
        rlProtocol = findViewById(R.id.llb_rl_protocol);
        checkBox = findViewById(R.id.llb_checkbox);
        tvLogin = findViewById(R.id.llb_tv_login);
        tvRegister = findViewById(R.id.llb_tv_register);
        tvProtocol.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                WebViewActivity.start(getContext(), InfoConstant.LOGIN_PROTOCOL_URL);
            }
        });
    }

    public void setProtocolGone() {
        rlProtocol.setVisibility(View.GONE);
    }

    public Observable<Boolean> getCheckBoxObservable() {
        return RxCompoundButton.checkedChanges(checkBox);
    }

    public void setLoginBtnEnable(boolean isEnable) {
        tvLogin.setEnabled(isEnable);
    }

    public void setRegister() {
        tvRegister.setVisibility(View.VISIBLE);
        tvRegister.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.start(getContext());
            }
        });
    }

    public void setOnLoginClickListener(String text, Consumer<Object> consumer) {
        tvLogin.setText(text);
        compositeDisposable.add(RxView.clicks(tvLogin).throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(consumer));
    }

    public void onDestroy() {
        if (null != compositeDisposable) {
            compositeDisposable.dispose();
            compositeDisposable = null;
        }
    }
}